package com.dswfort.novelSpider

import org.apache.commons.cli.*
import java.net.URI


fun main(args: Array<String>) {
    val parser = DefaultParser()
    val options = Options()
    options.addOption("h", "help", false, "帮助信息")
    options.addOption("url", "novelUrl", true, "指定小说地址")

    val commandLine: CommandLine = parser.parse(options, args)

    if (commandLine.hasOption("h")) {
        val formatter = HelpFormatter()
        formatter.printHelp("novelSpider", options)
        return
    }

    // 解析参数
    if (!commandLine.hasOption("url")) {
        println("未指定url参数")
        return
    }

    var novelUrl = commandLine.getOptionValue("url")

    // 初始化所有下载器
    val downloaders = listOf(BayizwDownloader(), BxwxDownloader())
    val downloaderMap: Map<String, BaseNovelDownloader> = downloaders.mapIndexed { index, value -> value.host to value }.toMap()

    val url = URI(novelUrl)
    if (url.host.isNullOrEmpty()) {
        println("非法的网址")
        return
    }

    val downloader: BaseNovelDownloader? = downloaderMap.getOrDefault(url.host, null)
    downloader?.beginDownload(novelUrl)
}